/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.spestimator;

import com.compomics.spestimator.files.fasta.FastaUnifier;
import com.compomics.spestimator.processes.RespinRunner;
import com.compomics.spestimator.processes.analysis.PeptideShakerPostProcessing;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.log4j.Logger;

/**
 *
 * @author Kenneth
 */
public class Spestimator {

    private static File fastaSpecies1;
    private static File fastaSpecies2;
    private static File parameterFile;
    private static File mgfFile;
    private static String projectID;
    private static File outputFolder;
    private static RespinRunner runner;
    private static File mergedFasta;
    private static Logger LOGGER = Logger.getLogger(Spestimator.class);
    private static String[] fastaFiles;
    private static String[] taxonomyIDs;

    public static void main(String[] args) {
        try {
            parseArgs(args);
            mergeFastas();
            runRespin();
            analyzeResults();
        } catch (Exception ex) {
            ex.printStackTrace();
            LOGGER.error(ex);
        }
    }

    private static void parseArgs(String[] args) {
        try {
            Options options = new Options();
            options.addOption("fasta_files", true, "user specified fasta files as comma separated list");
            options.addOption("taxonomy_IDs", true, "user specified taxonomyID's for species");
            options.addOption("param_file", true, "user specified searchparameters file (created by searchgui)");
            options.addOption("mgf_file", true, "user specified spectrum file");
            options.addOption("projectID", true, "the requested projectID");
            options.addOption("output_folder", true, "the directory all files should finally be stored");
            options.addOption("searchgui_path", true, "the filepath to the searchgui jar");
            options.addOption("peptideshaker_path", true, "the filepath to the peptideshaker jar");
            CommandLineParser parser = new BasicParser();
            CommandLine cmd = parser.parse(options, args);

            if (cmd.hasOption("fasta_files")) {
                fastaFiles = cmd.getOptionValue("fasta_files").split(",");
            }
            if (cmd.hasOption("taxonomy_IDs")) {
                taxonomyIDs = cmd.getOptionValue("taxonomy_IDs").split(",");
            }
            if (cmd.hasOption("param_file")) {
                parameterFile = new File(cmd.getOptionValue("param_file"));
            }
            if (cmd.hasOption("mgf_file")) {
                mgfFile = new File(cmd.getOptionValue("mgf_file"));
            }
            if (cmd.hasOption("projectID")) {
                projectID = cmd.getOptionValue("projectID");
            }
            if (cmd.hasOption("output_folder")) {
                outputFolder = new File(cmd.getOptionValue("output_folder"));
                mergedFasta = new File(outputFolder, projectID + "_merged_concatenated_target_decoy.FASTA");
                if (!outputFolder.exists()) {
                    outputFolder.mkdirs();
                }
            }
        } catch (ParseException ex) {
            LOGGER.error(ex);
        }
    }

    private static void mergeFastas() throws IOException {
        //make a hashmap
        if (fastaFiles.length != taxonomyIDs.length) {
            LOGGER.error("Fastafiles and taxonomyIDs differ in length !");
            System.exit(0);
        }
        HashMap<String, String> speciesMap = new HashMap<>();
        for (int i = 0; i < fastaFiles.length; i++) {
            speciesMap.put(fastaFiles[i], taxonomyIDs[i]);
        }
        FastaUnifier.makeUnifiedFasta(speciesMap, mergedFasta);
    }

    private static void runRespin() {
        runner = new RespinRunner(outputFolder, mergedFasta, mgfFile, parameterFile, projectID);
        runner.run();
    }

    private static void analyzeResults() throws IOException, InterruptedException {
        PeptideShakerPostProcessing.createReport(runner.getCpsOutputFile(), outputFolder);
    }
}
